<template>
  <div>
    <!-- 大客户订单提报 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjTransferred"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新增</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)">数据导入</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(3)" :disabled="isOpen">批量提交</multiuse-button>
      </template>
    </FilterTable>
    <!-- 未提交的才能取消 -->
    <BaseDialog :visible.sync="issearch" :title="'选择经销商'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isAdd" :title="'新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel @close="AddcloseHandler" />
    </BaseDialog>

    <BaseDialog :visible.sync="isemit" :title="'调拨审核'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="visible" :title="'导入'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <importFiles :rowData="formData" @close="importcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, vehicleConfiguration, updateAllocateApply } from '@/api/dealerSale/TransferManager/Transferred/index.js';
import { dictionary } from '@/utils/dictMixin';
import addModel from './addModel';
import emitModel from './emitModel';
import ownerSeacher from '../ownerSeacher';
import importFiles from './importFiles';
import Cookie from 'js-cookie';
import { getAppId } from '@/assets/js/auth'; //获取登录用户

export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        fromDealer: Cookie.get('ownerCode'),
        statusList: [60121001, 60121004]
      },
      fetchApi: getList,
      formData: {},
      issearch: false,
      isAdd: false,
      isemit: false,
      visible: false,
      selectList: [],
      isOpen: true,
      toDealer: ''
    };
  },
  mixins: [dictionary],
  components: {
    ownerSeacher,
    emitModel,
    addModel,
    importFiles
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    if(getAppId()=='beijing'){
      this.isOpen = false;
    }
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'applyNo',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: [
            { text: '未提交', value: 60121001 },
            { text: '审核驳回', value: 60121004 }
          ]
        },
        {
          type: 'INPUT',
          label: '调入方',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.toDealer = '';
            }
          }
        },
        {
          type: 'INPUT',
          label: '调拨人',
          fieldName: 'applyPersonName',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '调出申请时间',
          fieldName: 'startApplyDate|endApplyDate',
          placeholder: '请选择',
          initialValue: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 120,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} disabled={!(props.row.applyStatus == 60121001 || props.row.applyStatus == 60121004)}>
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  取消
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '未提交', value: 60121001 },
            { text: '审核驳回', value: 60121004 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '6012')}</span>;
          }
        },
        {
          title: '调拨单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '调出方',
          dataIndex: 'fromDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方',
          dataIndex: 'toDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方经销商代码',
          dataIndex: 'toDealer',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方申请时间',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调拨人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出说明',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let is = false;
        row.map(x => {
          if (!(x.applyStatus == 60121001 || x.applyStatus == 60121004)) {
            is = true;
          }
        });
        this.selectList = row;
        this.isOpen = is ? true : false;
      } else {
        this.selectList = [];
        this.isOpen = true;
        if (getAppId() == 'beijing') {
          this.isOpen = false;
        }
      }
    },
    seacherFun() {
      this.issearch = true;
    },
    async addHandle(type) {
      if (type == 1) {
        this.isAdd = true;
      } else if (type == 2) {
        this.visible = true;
      } else {
        let warnStr = '';
        if ((!this.selectList || this.selectList.length == 0) && getAppId() == 'beijing') {
          warnStr = '所有数据';
        }
        await this.$confirm('是否确定提交' + warnStr + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(x.id);
            });
            let isAll = 0;
            if (warnStr != '') {
              isAll = 1;
            }
            let res = await updateAllocateApply({ ids: arr, applyStatus: 60121002, isAll: isAll });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '提交成功',
                type: 'success'
              });
              this.fetchParams.toDealer = this.toDealer;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    changeHandle(params) {
      this.fetchParams.toDealer = this.toDealer;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data, type) {
      if (type == 1) {
        this.formData = data;
        this.isemit = true;
      } else {
        await this.$confirm('是否取消?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        })
          .then(async () => {
            let json = {
              id: data.id,
              applyStatus: 60121006
            };
            let res = await updateAllocateApply(json);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '取消成功',
                type: 'success'
              });
              this.isOpen = true;
              this.fetchParams.toDealer = this.toDealer;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    ownercloseHandler(val) {
      if (val) {
        console.log(val);
        this.topFilterList.map(x => {
          if (x.fieldName == 'dealerName') {
            x.initialValue = val.dealerName;
            this.toDealer = val.dealerCode;
          }
        });
      }
      this.issearch = false;
    },
    emitcloseHandler(val) {
      this.isemit = false;
      if (val) {
        this.fetchParams.toDealer = this.toDealer;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    AddcloseHandler(val) {
      this.isAdd = false;
      if (val) {
        this.fetchParams.toDealer = this.toDealer;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    importcloseHandler() {
      this.visible = false;
      this.fetchParams.toDealer = this.toDealer;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    resetChange(val) {
      this.toDealer = '';
      this.fetchParams.toDealer = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
